//
//  VCOfRegister.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/28.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "XYMacro.h"

@interface VCOfRegister ()
@property (nonatomic, strong) UITextField *phoneNumberField;
@property (nonatomic, strong) UITextField *codeField;
@property (nonatomic, strong) UIButton *checkCodeButton;
@property (nonatomic, strong) UIButton *referButton;
@property (nonatomic, strong) NSString *promptString;
@property (nonatomic, strong) NSString *bodyString;
@property (nonatomic, assign) BOOL isMatch;
@end

@implementation VCOfRegister

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNavigation];
    [self configViewController];
}



- (void)sendGetMessageCodeRequest {
    NSString *str1 = @"json=%7B%22isJoin%22%3A1%2C%22phone%22%3A%22";
    NSString *str2 = [str1 stringByAppendingString:_phoneNumberField.text];
    _bodyString = [str2 stringByAppendingString:@"%22%7D"];
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/user/reqcode.jhtml" andBody:_bodyString andBodyStyle:XYBodyString andHeader:@{@"Content-Type": @"application/x-www-form-urlencoded"} andResponse:XYJSON andSuccessBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"status"];
        ModelOfMineOne *model = [ModelOfMineOne modelWithDictionary:dic];
        if (model.succeed == 0) {
            _promptString = model.error_desc;
            [self createPromptBox];
        }
        
    } andFailureBlock:^(NSError *error) {}];
}

- (void)sendAfterWriteMessageCodeRequest {
    
    NSString *str1 = @"json=%7B%22idcode%22%3A%22";
    NSString *str2 = [str1 stringByAppendingString:_codeField.text];
    NSString *str3 = [str2 stringByAppendingString:@"%22%2C%22phone%22%3A%22"];
    NSString *str4 = [str3 stringByAppendingString:_phoneNumberField.text];
    _bodyString = [str4 stringByAppendingString:@"%22%7D"];
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/user/checkcode.jhtml" andBody:_bodyString andBodyStyle:XYBodyString andHeader:@{@"Content-Type": @"application/x-www-form-urlencoded"} andResponse:XYJSON andSuccessBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"status"];
        ModelOfMineOne *model = [ModelOfMineOne modelWithDictionary:dic];
        NSLog(@"%@", result);
        if (model.succeed == 1) {
            VCOfRegisterSecond *registerSecond = [[VCOfRegisterSecond alloc] init];
            registerSecond.phoneNumber = _phoneNumberField.text;
            [self.navigationController pushViewController:registerSecond animated:YES];

            } else {
                _promptString = model.error_desc;
                [self createPromptBox];
                if (model.succeed == 0) {
                    _promptString = model.error_desc;
                    [self createPromptBox];
            }
        }
    } andFailureBlock:^(NSError *error) {}];
}

- (void)configNavigation {
    self.title = @"注册";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"go_back"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 30, 30);
    [backButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}

- (void)configViewController {
    self.view.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.12 alpha:1.00];
    
    UIFont *newFont = [UIFont systemFontOfSize:14];
    UIColor *newColor = [UIColor colorWithRed:0.25 green:0.30 blue:0.36 alpha:1.00];

    UIView *customView = [[UIView alloc] init];
    customView.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.09 alpha:1.00];
    [self.view addSubview:customView];
    [customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.height.mas_equalTo(100);
        make.top.equalTo(self.view.mas_top).with.offset(30);
    }];
    
    UIView *lineTop = [[UIView alloc] init];
    lineTop.backgroundColor = [UIColor colorWithRed:0.14 green:0.13 blue:0.18 alpha:1.00];
    [customView addSubview:lineTop];
    [lineTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customView.mas_top).with.offset(0);
        make.right.equalTo(customView.mas_right).with.offset(0);
        make.left.equalTo(customView.mas_left).with.offset(0);
        make.height.mas_equalTo(1);
    }];
    
    UIView *lineBottom = [[UIView alloc] init];
    lineBottom.backgroundColor = [UIColor colorWithRed:0.14 green:0.13 blue:0.18 alpha:1.00];
    [customView addSubview:lineBottom];
    [lineBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(customView.mas_bottom).with.offset(0);
        make.left.equalTo(customView.mas_left).with.offset(0);
        make.right.equalTo(customView.mas_right).with.offset(0);
        make.height.mas_equalTo(1);
    }];
    
    UIView *lineMiddle = [[UIView alloc] init];
    lineMiddle.backgroundColor = [UIColor colorWithRed:0.84 green:0.00 blue:0.00 alpha:1.00];
    [customView addSubview:lineMiddle];
    [lineMiddle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customView.mas_top).with.offset(49.5);
        make.left.equalTo(customView.mas_left).with.offset(10);
        make.right.equalTo(customView.mas_right).with.offset(-10);
        make.height.mas_equalTo(1);
    }];
    
    _phoneNumberField = [[UITextField alloc] init];
    _phoneNumberField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号码" attributes:@{NSForegroundColorAttributeName:newColor, NSFontAttributeName:newFont}];
    _phoneNumberField.textColor = [UIColor whiteColor];
    [customView addSubview:_phoneNumberField];
    [_phoneNumberField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customView.mas_top).with.offset(1);
        make.left.equalTo(customView.mas_left).with.offset(40);
        make.right.equalTo(customView.mas_right).with.offset(-90);
        make.height.mas_equalTo(47);
    }];
    
    _codeField = [[UITextField alloc] init];
    _codeField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"短信验证码" attributes:@{NSForegroundColorAttributeName:newColor, NSFontAttributeName:newFont}];
    _codeField.textColor = [UIColor whiteColor];
    [customView addSubview:_codeField];
    [_codeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customView.mas_top).with.offset(51);
        make.left.equalTo(customView.mas_left).with.offset(40);
        make.right.equalTo(customView.mas_right).with.offset(-40);
        make.height.mas_equalTo(47);
    }];
    
    _checkCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_checkCodeButton setBackgroundImage:[UIImage imageNamed:@"get_code_bg"] forState:UIControlStateNormal];
    [_checkCodeButton setTitle:@"获取手机验证码" forState:UIControlStateNormal];
    _checkCodeButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_checkCodeButton addTarget:self action:@selector(getMessageCode:) forControlEvents:UIControlEventTouchUpInside];
    [_checkCodeButton setTitleColor:[UIColor colorWithRed:0.90 green:0.00 blue:0.00 alpha:1.00] forState:UIControlStateNormal];
    [customView addSubview:_checkCodeButton];
    [_checkCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(customView.mas_top).with.offset(10);
        make.right.equalTo(customView.mas_right).with.offset(-5);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(100);
    }];
    
    _referButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _referButton.backgroundColor = [UIColor colorWithRed:1.00 green:0.00 blue:0.00 alpha:1.00];
    [_referButton setTitle:@"提交" forState:UIControlStateNormal];
    [_referButton setTintColor:[UIColor whiteColor]];
    _referButton.layer.cornerRadius = 5;
    [_referButton addTarget:self action:@selector(refer:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_referButton];
    [_referButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customView.mas_bottom).with.offset(40);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.right.equalTo(self.view.mas_right).with.offset(-30);
        make.height.mas_equalTo(30);
    }];
}


- (void)goBack:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)refer:(UIButton *)button {
//    VCOfRegisterSecond *registerSecond = [[VCOfRegisterSecond alloc] init];
//    registerSecond.phoneNumber = _phoneNumberField.text;
//    [self.navigationController pushViewController:registerSecond animated:YES];

    
    if ([_codeField.text isEqualToString:@""] || [_phoneNumberField.text isEqualToString:@""] ) {
        _promptString = @"您先获取短信验证码";
        [self createPromptBox];
    } else {
        [self sendAfterWriteMessageCodeRequest];
        
    }
}

- (void)getMessageCode:(UIButton *)button {
    //布尔值赋值
    _isMatch = [self checkTelNumber:_phoneNumberField.text];
    if (_isMatch == 0) {
        _promptString = @"您输入的手机号码有误, 请重新输入";
        [self createPromptBox];
    } else {
        [self sendGetMessageCodeRequest];
    }
}

#pragma mark ----------正则判断手机号码-----------------
- (BOOL)checkTelNumber:(NSString *) telNumber {
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:telNumber];
}

#pragma mark -----------创建自定义提示栏-----------------
- (void)createPromptBox {
    UIWindow *windows = [UIApplication sharedApplication].keyWindow;
    UIView *customPromptBox = [[UIView alloc]init];
    customPromptBox.backgroundColor = [UIColor colorWithRed:0.18 green:0.18 blue:0.18 alpha:1.00];
    [windows addSubview:customPromptBox];
    [customPromptBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.centerY.mas_equalTo(self.view.mas_centerY);
        make.width.mas_offset(260);
        make.height.mas_offset(100);
    }];
    customPromptBox.layer.masksToBounds = YES;
    customPromptBox.layer.cornerRadius = 8.0f;
    
    UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
    [customPromptBox addSubview:logoImageView];
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customPromptBox.mas_top).with.offset(10);
        make.centerX.equalTo(customPromptBox.mas_centerX);
        make.height.mas_offset(40);
        make.width.mas_offset(40);
    }];
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.text = _promptString;
    textLabel.font = [UIFont systemFontOfSize:14];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [customPromptBox addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logoImageView.mas_bottom).with.offset(5);
        make.bottom.equalTo(customPromptBox.mas_bottom).with.offset(-5);
        make.left.equalTo(customPromptBox.mas_left).with.offset(0);
        make.right.equalTo(customPromptBox.mas_right).with.offset(0);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
        //2秒以后移除view
        [customPromptBox removeFromSuperview];
    });
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
